<template>
    <el-form label-width="100px">
        <el-form-item label="SKU名称">
            <el-input placeholder="SKU名称" v-model="skuParams.skuName"></el-input>
        </el-form-item>
        <el-form-item label="价格(元)">
            <el-input placeholder="价格(元)" type="number" v-model="skuParams.price"></el-input>
        </el-form-item>
        <el-form-item label="重量(g)">
            <el-input placeholder="重量(g)" type="number" v-model="skuParams.weight"></el-input>
        </el-form-item>
        <el-form-item label="SKU描述">
            <el-input placeholder="SKU描述" type="textarea" v-model="skuParams.skuDesc"></el-input>
        </el-form-item>
        <el-form-item label="平台属性">
            <el-form :inline="true">
                <el-form-item v-for="(item, index) in platformAttr" :key="item.id" :label="item.attrName">
                    <el-select v-model="item.platformAttrArr">
                        <el-option :value="`${item.id}:${attrValue.id}`" v-for="(attrValue, index) in item.attrValueList"
                            :key="attrValue.id" :label="attrValue.valueName"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
        </el-form-item>
        <el-form-item label="销售属性">
            <el-form :inline="true">
                <el-form-item :label="item.saleAttrName" v-for="(item, index) in saleAttr" :key="item.id">
                    <el-select v-model="item.saleAttrArr">
                        <el-option :value="`${item.id}:${saleAttrValue.id}`"
                            v-for="(saleAttrValue, index) in item.spuSaleAttrValueList" :key="saleAttrValue.id"
                            :label="saleAttrValue.saleAttrValueName"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
        </el-form-item>
        <el-form-item label="图片名称">
            <el-table border :data="imgList" ref="table">
                <el-table-column type="selection" width="80px" align="center"></el-table-column>
                <el-table-column label="图片">
                    <template #="{ row, $index }">
                        <img :src="row.imgUrl" alt="" style="width:100px;height: 100px;">
                    </template>
                </el-table-column>
                <el-table-column label="名称" prop="imgName"></el-table-column>
                <el-table-column label="操作">
                    <template #="{ row, $index }">
                        <el-button type="primary" size="small" @click="handler(row)">设置默认</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" size="default" @click="save">保存</el-button>
            <el-button type="primary" size="default" @click="cancel">取消</el-button>
        </el-form-item>
    </el-form>
</template>

<script setup lang="ts">
import { ref,reactive } from 'vue';
import {reqSpuHasSaleAttr,reqSpuImageList,reqAddSku} from '@/api/product/spu/index'
import type {SaleAttrResponseData,SaleAttr,SpuHasImg,SpuImg,SkuData} from '@/api/product/spu/type'
import {reqAttr} from '@/api/product/attr/index'
import type {AttrResponseData,Attr} from '@/api/product/attr/type'
import { ElMessage } from 'element-plus';
let $emit = defineEmits(['changeSence'])
let saleAttr = ref<SaleAttr[]>([])
let imgList = ref<SpuImg[]>([])
let platformAttr = ref<Attr[]>([])
//获取table组件实例
let table = ref<any>();
//收集SKU的参数
let skuParams = reactive<SkuData>({
    //父组件传递过来的数据
    "category3Id": "",//三级分类的ID
    "spuId": "",//已有的SPU的ID
    "tmId": "",//SPU品牌的ID
    //v-model收集
    "skuName": "",//sku名字
    "price": "",//sku价格
    "weight": "",//sku重量
    "skuDesc": "",//sku的描述

    "skuAttrValueList": [//平台属性的收集
    ],
    "skuSaleAttrValueList": [//销售属性
    ],
    "skuDefaultImg": "",//sku图片地址
})
// 取消
const cancel = ()=>{
    $emit('changeSence',{flag:0,type:'update'})
}
const initSkuData = async (c1Id:number,c2Id:number,data:any)=>{
    skuParams.category3Id = data.category3Id
    skuParams.tmId = data.tmId
    skuParams.spuId = data.id
    // 销售属性
    let result:SaleAttrResponseData = await reqSpuHasSaleAttr(data.id)
    saleAttr.value = result.data
    // 平台属性
    let result2:AttrResponseData = await reqAttr(c1Id,c2Id,data.category3Id)
    platformAttr.value = result2.data
    // 照片
    let result3:SpuHasImg = await reqSpuImageList(data.id)
    imgList.value = result3.data
}
// 设置默认
const handler = (row:SpuImg)=>{
    //点击的时候,全部图片的的复选框不勾选
    imgList.value.forEach(item=>{
        table.value.toggleRowSelection(item,false)
    })
    //选中的图片才勾选
    table.value.toggleRowSelection(row,true)
    skuParams.skuDefaultImg = (row.imgUrl as string)
}
const save= async ()=>{
    // skuParams.skuAttrValueList = []
    // platformAttr.value.forEach(item=>{
    //     if(item.platformAttrArr){
    //         let result = item.platformAttrArr.split(':')
    //         let newParams = {
    //             attrId:result[0],
    //             valueId:result[1]
    //         }
    //         skuParams.skuAttrValueList.push(newParams)
    //     }
    // })
    // console.log(skuParams.skuAttrValueList)
    // 平台属性
    skuParams.skuAttrValueList = platformAttr.value.reduce((pre:any,next:any)=>{
        if(next.platformAttrArr){
            let result = next.platformAttrArr.split(':')
            let newParams = {
                attrId:result[0],
                valueId:result[1]
            }
            pre.push(newParams)
        }
        return pre
    },[])
    // 销售属性
    skuParams.skuSaleAttrValueList = saleAttr.value.reduce((pre:any,next:any)=>{
        if(next.saleAttrArr){
            let result = next.saleAttrArr.split(':')
            let newParams = {
                saleAttrId:result[0],
                saleAttrValueId:result[1]
            }
            pre.push(newParams)
        }
        return pre
    },[])
    let result = await reqAddSku(skuParams)
    if(result.code==200){
        ElMessage({
            type:'success',
            message:'添加成功'
        })
        $emit('changeSence',{flag:0,type:''})
    }else{
        ElMessage({
            type:'success',
            message:'添加失败'
        })
    }
}
defineExpose({initSkuData})
</script>

<style scoped></style>